package com.hz.control;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import com.hz.dbconn.DBConnect;
import com.hz.model.ApplyAct;
import com.hz.util.Pagination;

public class ApplyActControl {
    public static Pagination pageInstance = null;
	
       /*
        * 新增活动
        * 参数（ApplyAct）
        * 返回值：boolean
        */
	public static boolean insertApplyAct(ApplyAct activity){
		DBConnect dbc = new DBConnect();
		String sql = "insert into applyact(actType,name,purpose,object,leader," +
				"contact,procedures,budget,notes,userId,holdTime,state)values(?,?,?,?,?,?,?,?,?,?,?,?)";
		dbc.prepareStatement(sql);
		try{
		       int index = 1;
		       dbc.setInt(index++, activity.getActType());
		       dbc.setString(index++, activity.getName());
		       dbc.setString(index++,activity.getPurpose());
		       dbc.setString(index++,activity.getObject());
		       dbc.setString(index++,activity.getLeader());
		       dbc.setString(index++,activity.getContact());
		       dbc.setString(index++,activity.getProcedure());
		       dbc.setString(index++,activity.getBudget());
		       dbc.setString(index++,activity.getNotes());
		       dbc.setInt(index++,activity.getUserId());
		       dbc.setString(index++,activity.getHoldTime());
		       dbc.setInt(index++,0);//新增活动为待审核
			dbc.executeUpdate();
			return true;
		}catch(SQLException e){
			e.printStackTrace();
			System.out.println("查询数据库，插入申请活动失败!");
			return false;
		}finally{
			dbc.close();
		}
	}



 
              /*根据id获取活动信息
	        * 参数（id）
	        * 返回值：ApplyAct
	        */
        public static  ApplyAct getActivityInformation(int id){
	      DBConnect dbc = new DBConnect();
	      String sql = "SELECT * FROM applyact WHERE id='"+id+"'";
	      dbc.prepareStatement(sql);
	      try{
		    ApplyAct activity=new ApplyAct();
	            ResultSet rs = dbc.executeQuery();
	           if(rs.next())
	            {
	             activity.setId(id);
		         activity.setName(rs.getString("name"));
		         activity.setActType(rs.getInt("actType"));
		         activity.setPurpose(rs.getString("purpose"));
	   	         activity.setObject(rs.getString("object"));
	 	         activity.setLeader(rs.getString("leader"));
	 	         activity.setContact(rs.getString("contact"));
	 	         activity.setProcedure(rs.getString("procedures"));
	 	         activity.setBudget(rs.getString("budget"));
	 	         activity.setNotes(rs.getString("notes"));
	 	         activity.setUserId(rs.getInt("userId"));
	 	         activity.setState(rs.getInt("state"));
	 	         activity.setHoldTime(rs.getString("holdTime"));
	 	         return activity;
	             }
            } catch (SQLException e) {
	             e.printStackTrace();
	             System.out.println("获取申请活动信息失败!");
	             return null;
              } finally {
	                    dbc.close();
             }
            return null;
   }
   
       /*删除活动
        * 参数（id）
        * 返回值：boolean
        */
        public  static boolean deleteActivity(int id){
	     DBConnect dbc = new DBConnect();
	     String sql = "DELETE FROM applyact WHERE id=?";
	     dbc.prepareStatement(sql);
	     try {
		   dbc.setInt(1, id);
		   dbc.executeUpdate();
		   return true;
	      } catch (SQLException e) {
		   e.printStackTrace();
		   return false;
	     } finally {
		dbc.close();
	  }
   }
  
           /*修改活动内容
            * 参数（ApplyAct,id）
            * 返回值：boolean
            */
           public static  boolean updateApplyAct(ApplyAct activity,int id){
        	DBConnect dbc = new DBConnect();
	        String sql = "UPDATE applyact SET actType=?,name=?,purpose=?,object=?,leader=?," +
	        	      "contact=?,procedures=?,budget=?,notes=?,userId=?,holdTime=?,state=? WHERE id=?";
	        dbc.prepareStatement(sql);
	        try {  
	               int index = 1;
	               dbc.setInt(index++, activity.getActType());
		       dbc.setString(index++, activity.getName());
		       dbc.setString(index++,activity.getPurpose());
		       dbc.setString(index++,activity.getObject());
		       dbc.setString(index++,activity.getLeader());
		       dbc.setString(index++,activity.getContact());
		       dbc.setString(index++,activity.getProcedure());
		       dbc.setString(index++,activity.getBudget());
		       dbc.setString(index++,activity.getNotes());
		       dbc.setInt(index++,activity.getUserId());
		       dbc.setString(index++,activity.getHoldTime());
		       dbc.setInt(index++,0);//修改后为待审核
		       dbc.setInt(index++, id);
		       dbc.executeUpdate();
		       return true;
	           } catch (SQLException e) {
		        e.printStackTrace();
		        System.out.println("更新申请活动信息失败");
		       return false;
	          } finally {
		       dbc.close();
	    }
   }
           /*审核申请活动
            * 参数（活动id,审核结果state）
            * 返回值：boolean
            */
           public static  boolean controlApplyAct(int state,int id){
        	DBConnect dbc = new DBConnect();
	        String sql = "UPDATE applyact SET state='"+state+"' WHERE id='"+id+"'";
	        System.out.println(sql);
	        dbc.prepareStatement(sql);
	        try {  
	               
		       dbc.executeUpdate();
		       return true;
	           } catch (SQLException e) {
		        e.printStackTrace();
		        System.out.println("管理申请活动信息失败");
		       return false;
	          } finally {
		       dbc.close();
	    }
   }
         
          
           
   //多条件搜索提交申请的活动
public static Collection getApplyActivities(String actType,String state,String holdTime,String userId,int page, int rowsPerPage) {
    String temp = " WHERE ";
	String clause = temp;
	String sql ="";
     //System.out.println(userId);
	if (actType != null&&!actType.equals(""))
		clause = clause + "actType = '" + actType + "' ";
	
	if (!clause.equals(temp) && state != null&&!state.equals(""))
		clause = clause + " and state like '" + state + "' ";

	if (clause.equals(temp) && state != null&&!state.equals(""))
		clause = clause + "state like '" + state + "' ";

	if (!clause.equals(temp) && holdTime != null&&!holdTime.equals(""))
		clause = clause + " and holdTime like '%" + holdTime + "%' ";

	if (clause.equals(temp) && holdTime != null&&!holdTime.equals(""))
		clause = clause + "holdTime like '%" + holdTime + "%' ";

	if (!clause.equals(temp) && userId != null&&!userId.equals(""))
		clause = clause + " and userId = '" + userId + "'";

	if (clause.equals(temp) && userId != null&&!userId.equals(""))
		clause = clause + "userId = '" + userId + "' ";

	if (clause.equals(temp)) {
		clause = " ";
	}
	
	
	sql = "select * from applyact " + clause ;
	System.out.println(sql);
	pageInstance = new ApplyActControlPage(sql, rowsPerPage);
	try {
	     return pageInstance.getPage(page);
	}catch (Exception e) {
		e.printStackTrace();
	}
	return null;
}

}

class ApplyActControlPage extends Pagination {
	
	public ApplyActControlPage(String sql, int rowsPerPage) {
		super(sql, rowsPerPage, 1);
	}

	public Collection<ApplyAct> packResultSet(ResultSet rs) {
		Collection<ApplyAct> actList = new ArrayList<ApplyAct>();
		try {
			while (rs.next()) {
			         ApplyAct activity = new ApplyAct();
			         activity.setId(rs.getInt("id"));
			         activity.setName(rs.getString("name"));
			         activity.setActType(rs.getInt("actType"));
			         activity.setPurpose(rs.getString("purpose"));
		   	         activity.setObject(rs.getString("object"));
		 	         activity.setLeader(rs.getString("leader"));
		 	         activity.setContact(rs.getString("contact"));
		 	         activity.setProcedure(rs.getString("procedures"));
		 	         activity.setBudget(rs.getString("budget"));
		 	         activity.setNotes(rs.getString("notes"));
		 	         activity.setUserId(rs.getInt("userId"));
		 	         activity.setHoldTime(rs.getString("holdTime"));
		 	         activity.setState(rs.getInt("state"));
				 actList.add(activity);
				 activity = null;
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}
		return actList;
	}
   
   
}

